package com.interview.a_多线程.d_六种状态.Blocked_Wating_TimeWating;

public class ThreadStatus implements Runnable {

    @Override
    public void run() {
        getLock();
    }

    private synchronized void getLock() {
        try {
            System.out.println(Thread.currentThread().getName() + " getLock");
            Thread.sleep(2000);
            wait();
        } catch (InterruptedException e) {
            e.printStackTrace();
        }

    }


    public static void main(String[] args) throws InterruptedException {
        ThreadStatus threadStatus = new ThreadStatus();

        Thread thread1 = new Thread(threadStatus);
        thread1.setName("线程1");
        thread1.start();

        Thread thread2 = new Thread(threadStatus);
        thread2.setName("线程2");
        thread2.start();

        //线程1先启动 进入同步锁方法 持有锁  并阻塞sleep（时间）
        //所以是 TIMEW_WATING
        Thread.sleep(200);
        System.out.println("thread1 " + thread1.getState());

        // 线程2 启动 线程1没有释放锁 所以获取不到锁
        //      此时是 BLOCKED
        System.out.println("thread2 " + thread2.getState());

        // 因为执行了 wait 并且没被唤醒 所以是 WATING
        Thread.sleep(2200);
        System.out.println("thread1 " + thread1.getState());


    }
}
